package com.xxc.hs;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xxc.hs.fragment.HomeFragment;
import com.xxc.hs.fragment.SafeFragment;
import com.xxc.hs.fragment.SourceTraceFragment;
import com.xxc.hs.fragment.UserFragment;
import com.xxc.hs.fragment.WaterFragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {
    private HomeFragment homeFragment;
    private SafeFragment safeFragment;
    private WaterFragment waterFragment;
    private UserFragment userFragment;
    private SourceTraceFragment productFragment;
    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            bottomNavigationView = findViewById(R.id.bottomNavigationView);
            bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    if (item.getItemId() == R.id.home) {
                        selectedFragment(0);
                    } else if (item.getItemId() == R.id.safe) {
                        selectedFragment(1);
                    } else if (item.getItemId() == R.id.water) {
                        selectedFragment(2);
                    } else if (item.getItemId() == R.id.product) {
                        selectedFragment(3);
                    } else if (item.getItemId() == R.id.mine) {
                        selectedFragment(4);
                    }
                    return true;
                }
            });

            //默认首页选中
            selectedFragment(0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void selectedFragment(int position) {
        try {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            hideFragment(fragmentTransaction);
            if (position == 0) {
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.content, homeFragment);
                } else {
                    fragmentTransaction.show(homeFragment);
                }
            } else if (position == 1) {
                if (safeFragment == null) {
                    safeFragment = new SafeFragment();
                    fragmentTransaction.add(R.id.content, safeFragment);
                } else {
                    fragmentTransaction.show(safeFragment);
                }
            } else if (position == 2) {
                if (waterFragment == null) {
                    waterFragment = new WaterFragment();
                    fragmentTransaction.add(R.id.content, waterFragment);
                } else {
                    fragmentTransaction.show(waterFragment);
                }
            } else if (position == 3) {
                if (productFragment == null) {
                    productFragment = new SourceTraceFragment();
                    fragmentTransaction.add(R.id.content, productFragment);
                } else {
                    fragmentTransaction.show(productFragment);
                }
            } else if (position == 4) {
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    fragmentTransaction.add(R.id.content, userFragment);
                } else {
                    fragmentTransaction.show(userFragment);
                }
            }
            fragmentTransaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        try {
            if (homeFragment != null) {
                fragmentTransaction.hide(homeFragment);
            }
            if (safeFragment != null) {
                fragmentTransaction.hide(safeFragment);
            }
            if (waterFragment != null) {
                fragmentTransaction.hide(waterFragment);
            }
            if (productFragment != null) {
                fragmentTransaction.hide(productFragment);
            }
            if (userFragment != null) {
                fragmentTransaction.hide(userFragment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            removeAllFragments();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 移除并释放所有 Fragment 资源
     */
    private void removeAllFragments() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        try {
            if (homeFragment != null) {
                homeFragment.onDestroy();
                transaction.remove(homeFragment);
                homeFragment = null;
            }

            if (safeFragment != null) {
                safeFragment.onDestroy();
                transaction.remove(safeFragment);
                safeFragment = null;
            }

            if (waterFragment != null) {
                waterFragment.onDestroy();
                transaction.remove(waterFragment);
                waterFragment = null;
            }

            if (productFragment != null) {
                productFragment.onDestroy();
                transaction.remove(productFragment);
                productFragment = null;
            }

            if (userFragment != null) {
                userFragment.onDestroy();
                transaction.remove(userFragment);
                userFragment = null;
            }
            transaction.commitNowAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}